package org.cos.verifier.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class LogDlg extends Frame {
	TextArea log_area = null;
	LogDlg me = null;
	Button hide = null;
	Button clear = null;
	Panel ctrl_panel;

	static LogDlg instance = null;
	static LogDlg get_inst (){
		if (instance == null) {
			instance = new LogDlg();
		}
		return instance;
	}
	
	public LogDlg() {
		this.me = this;
		log_area = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);
		hide = new Button("关闭");
		clear = new Button("清空");
		ctrl_panel = new Panel();
		ctrl_panel.setLayout(new GridLayout(2,2));
		ctrl_panel.add(hide);
		ctrl_panel.add(clear);
		ctrl_panel.add(new Label());
		
		hide.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				me.setVisible(false);
			}});
		me.setSize(400, 500);
		
		me.setTitle("CosVerifier操作日志");
		me.add(log_area, BorderLayout.CENTER);
		me.add(ctrl_panel, BorderLayout.SOUTH);
		me.addWindowListener(new FrameHandler());
	}
	
	public void log(String str) {
		log_area.append(get_crr_time() + str+"\n");
	}
	
	private String get_crr_time(){
		SimpleDateFormat sDateFormat = new SimpleDateFormat(
				"yyyyMMdd/hh-mm-ss:");
		String date = sDateFormat.format(new java.util.Date());
		return date;
	}
	
	public void clear() {
		log_area.setText("");
	}
	
	public void display() {
		this.setVisible(true);
	}
	
	class FrameHandler extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			me.setVisible(false);
		}
	}
	
	
}
